package com.itheima.service.Impl;

import com.itheima.entity.Emp;
import com.itheima.entity.EmpLoginInfo;
import com.itheima.mapper.LoginMapper;
import com.itheima.service.LoginService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    /**
     * 登录业务逻辑
     *
     * @param emp
     * @return
     */
    @Override
    public EmpLoginInfo login(Emp emp) {
        // TODO Auto-generated method stub

        EmpLoginInfo result = loginMapper.login(emp);

        if (result != null) {
            Map<String, Object> claims = new HashMap<>();
            //自定义有效载荷
            claims.put("id", result.getId());
            claims.put("name", result.getUsername());
            //调用JWT工具类生成令牌
            String jwt=JwtUtils.generateJwt(claims);

            //将令牌存入登录信息对象中
            result.setToken(jwt);
        }

        return result;
    }
}
